﻿using HFUTCourseSimulation.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HFUTCourseSimulation.Dialog {
    /// <summary>
    /// Interaction logic for Simulator.xaml
    /// </summary>
    public partial class Simulator : Window {
        public Simulator() {
            InitializeComponent();

            // Initialize container
            weekTextBlocks = new List<TextBlock>();
            courseBlocks = new List<Border>();
            currentWeek = 1;
        }

        public Kernel.Data.Presentation.Semester CurrentSemester { get; set; }
        private int currentWeek;
        private List<TextBlock> weekTextBlocks;
        private List<Border> courseBlocks;

        private void uiMainWindow_Loaded(object sender, RoutedEventArgs e) {
            // Initialize UI layout by semester
            // Setup grid rows and columns
            uiCoreGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) });
            for (int i = 0; i < 7; ++i) {
                uiCoreGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
            }
            uiCoreGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
            uiCoreGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) });
            for (int i = 0; i < CurrentSemester.indexCount; ++i) {
                uiCoreGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
            }
            // Set head bar
            var headbar = new Rectangle();
            headbar.Fill = new SolidColorBrush(ColorConsistency.HeadbarColor.Background);
            Grid.SetRow(headbar, 0);
            Grid.SetRowSpan(headbar, 2);
            Grid.SetColumn(headbar, 0);
            Grid.SetColumnSpan(headbar, 7 + 1);
            uiCoreGrid.Children.Add(headbar);
            // Setup week text block
            for (int week = 1; week <= 7; ++week) {
                var weekLabel = new TextBlock();
                weekLabel.Text = WeekNames.Names[week - 1];
                weekLabel.Foreground = new SolidColorBrush(ColorConsistency.HeadbarColor.Foreground);
                weekLabel.HorizontalAlignment = HorizontalAlignment.Center;
                Grid.SetRow(weekLabel, 0);
                Grid.SetColumn(weekLabel, week - 1 + 1);
                uiCoreGrid.Children.Add(weekLabel);

                var weekDateLabel = new TextBlock();
                weekDateLabel.Text = "N/A";
                weekDateLabel.Foreground = new SolidColorBrush(ColorConsistency.HeadbarColor.Foreground);
                weekDateLabel.HorizontalAlignment = HorizontalAlignment.Center;
                Grid.SetRow(weekDateLabel, 1);
                Grid.SetColumn(weekDateLabel, week - 1 + 1);
                uiCoreGrid.Children.Add(weekDateLabel);

                weekTextBlocks.Add(weekDateLabel);
            }
            // Setup sidebar and index text block
            for (int index = 1; index <= CurrentSemester.indexCount; ++index) {
                var color = ColorConsistency.GetSidebarColor(CurrentSemester.GetIndexKind(index));

                var sidebar = new Rectangle();
                sidebar.Fill = new SolidColorBrush(color.Background);
                Grid.SetColumn(sidebar, 0);
                Grid.SetRow(sidebar, index - 1 + 2);
                uiCoreGrid.Children.Add(sidebar);

                var indexLabel = new TextBlock();
                indexLabel.Text = index.ToString();
                indexLabel.Foreground = new SolidColorBrush(color.Foreground);
                indexLabel.VerticalAlignment = VerticalAlignment.Center;
                indexLabel.HorizontalAlignment = HorizontalAlignment.Center;
                Grid.SetColumn(indexLabel, 0);
                Grid.SetRow(indexLabel, index - 1 + 2);
                uiCoreGrid.Children.Add(indexLabel);
            }
            // Set corner
            var cornerLabel = new TextBlock();
            cornerLabel.Text = "节次";
            cornerLabel.HorizontalAlignment = HorizontalAlignment.Center;
            cornerLabel.VerticalAlignment = VerticalAlignment.Center;
            Grid.SetColumn(cornerLabel, 0);
            Grid.SetRow(cornerLabel, 0);
            Grid.SetRowSpan(cornerLabel, 2);
            uiCoreGrid.Children.Add(cornerLabel);
            // Add chessboard
            for (int week = 1; week <= 7; ++week) {
                for (int index = 1; index <= CurrentSemester.indexCount; ++index) {
                    if ((week + index) % 2 != 0) continue;

                    var chessboard = new Rectangle();
                    chessboard.Fill = new SolidColorBrush(ColorConsistency.ChessboardColor);
                    Grid.SetColumn(chessboard, week - 1 + 1);
                    Grid.SetRow(chessboard, index - 1 + 2);
                    uiCoreGrid.Children.Add((chessboard));
                }
            }

            // Render for week 1
            currentWeek = 1;
            RenderWeek();
        }

        private void uiMainWindow_Closed(object sender, EventArgs e) {
            // 清空模拟器内容
            uiCoreGrid.Children.Clear();
            uiCoreGrid.RowDefinitions.Clear();
            uiCoreGrid.ColumnDefinitions.Clear();

            weekTextBlocks.Clear();
            courseBlocks.Clear();
            currentWeek = 1;
        }

        private void RenderWeek() {
            // Clear current course blocks
            foreach (var block in courseBlocks) {
                uiCoreGrid.Children.Remove(block);
            }
            courseBlocks.Clear();

            // Create new course for this week
            var week = CurrentSemester.weeks[currentWeek - 1];
            for (int i = 0; i < 7; ++i) {
                var day = week.days[i];

                // 更新标题上的日期
                weekTextBlocks[i].Text = day.date;

                // 更新当天课程
                foreach (var lesson in day.lessons) {
                    var courseBlock = new Border();
                    courseBlock.ToolTip = lesson.description;
                    courseBlock.Background = new SolidColorBrush(lesson.color.Background);
                    courseBlock.CornerRadius = new CornerRadius(5);
                    Grid.SetColumn(courseBlock, i + 1);
                    Grid.SetRow(courseBlock, lesson.startIndex - 1 + 2);
                    Grid.SetRowSpan(courseBlock, lesson.indexSpan);
                    uiCoreGrid.Children.Add(courseBlock);
                    courseBlocks.Add(courseBlock);

                    var courseNameLabel = new TextBlock();
                    courseNameLabel.Text = lesson.name;
                    courseNameLabel.TextWrapping = TextWrapping.Wrap;
                    courseNameLabel.Foreground = new SolidColorBrush(lesson.color.Foreground);
                    courseNameLabel.HorizontalAlignment = HorizontalAlignment.Left;
                    courseNameLabel.VerticalAlignment = VerticalAlignment.Center;
                    courseBlock.Child = courseNameLabel;
                }
            }

            // 更新教学周序号
            uiWeek.Text = currentWeek.ToString();
            // 更新按钮启用情况
            uiBtnPrevWeek.IsEnabled = currentWeek > 1;
            uiBtnNextWeek.IsEnabled = currentWeek < CurrentSemester.weekCount;
        }

        private bool HasPrevWeek() {
            return currentWeek > 1;
        }

        private void PrevWeek() {
            if (HasPrevWeek()) {
                currentWeek--;
                RenderWeek();
            }
        }

        private bool HasNextWeek() {
            return currentWeek < CurrentSemester.weekCount;
        }

        private void NextWeek() {
            if (HasNextWeek()) {
                currentWeek++;
                RenderWeek();
            }
        }

        #region UI and Shotcuts

        private void uiBtnPrevWeek_Click(object sender, RoutedEventArgs e) {
            PrevWeek();
        }

        private void uiBtnNextWeek_Click(object sender, RoutedEventArgs e) {
            NextWeek();
        }

        private void commandBtnPrevWeek_Execute(object sender, ExecutedRoutedEventArgs e) => uiBtnPrevWeek_Click(sender, e);
        private void commandBtnNextWeek_Execute(object sender, ExecutedRoutedEventArgs e) => uiBtnNextWeek_Click(sender, e);

        private void commandBtnPrevWeek_CanExecute(object sender, CanExecuteRoutedEventArgs e) => e.CanExecute = HasPrevWeek();
        private void commandBtnNextWeek_CanExecute(object sender, CanExecuteRoutedEventArgs e) => e.CanExecute = HasNextWeek();

        #endregion

    }
}
